# -*- coding:utf-8 -*-
"""
@author: Jioccer
@time: 2023/9/9 13:03 
@file: demo2.py
@project: 一键关闭Windows更新
@describe: TODO
"""

from PySide6.QtWidgets import QApplication, QMessageBox, QWidget, QMainWindow
from PySide6.QtGui import QIcon, QPixmap
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile
import winreg
# 导入UI数据文件
from image_data import image_data
from UI.my_ui import Ui_Form

# 定义注册表键路径
key_path = r"SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"

class MyWindow(QMainWindow, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

class Windows:

    def __init__(self):
        # qfile_status = QFile("UI/untitled.ui")
        # qfile_status.open(QFile.ReadOnly)
        # qfile_status.close()
        #
        # self.window = QUiLoader().load(qfile_status)

        self.window = MyWindow()

        # 设置窗口无法改变大小
        self.window.setFixedSize(self.window.width(), self.window.height())

        self.window.In.clicked.connect(self.handleIn)
        self.window.Uin.clicked.connect(self.handleUin)

    def handleIn(self):
        try:
            # 打开注册表键
            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_WRITE) as key:
                # 定义要设置的注册表值
                registry_values = {
                    "FlightSettingsMaxPauseDays": 0x1b58,  # DWORD值
                    "PauseFeatureUpdatesStartTime": "2023-07-07T10:00:52Z",  # 字符串值
                    "PauseFeatureUpdatesEndTime": "2042-09-05T09:59:52Z",  # 字符串值
                    "PauseQualityUpdatesStartTime": "2023-07-07T10:00:52Z",  # 字符串值
                    "PauseQualityUpdatesEndTime": "2042-09-05T09:59:52Z",  # 字符串值
                    "PauseUpdatesStartTime": "2023-07-07T09:59:52Z",  # 字符串值
                    "PauseUpdatesExpiryTime": "2042-09-05T09:59:52Z",  # 字符串值
                }

                for value_name, value_data in registry_values.items():
                    if isinstance(value_data, int):
                        # 设置DWORD值
                        winreg.SetValueEx(key, value_name, 0, winreg.REG_DWORD, value_data)
                        print(f"已设置DWORD注册表值: {value_name}")
                    elif isinstance(value_data, str):
                        # 设置字符串值
                        winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value_data)
                        print(f"已设置字符串注册表值: {value_name}")
                    else:
                        print(f"不支持的注册表值类型: {value_name}")

            print("注册表操作执行完成")
            self.show_message("成功", "屏蔽更新成功")

        except Exception as e:
            print(f"发生错误: {e}")
            self.show_message("失败", str(e))

    def handleUin(self):
        try:
            # 打开注册表键
            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_WRITE) as key:

                # 删除指定的注册表值
                values_to_delete = [
                    "FlightSettingsMaxPauseDays",
                    "PauseFeatureUpdatesStartTime",
                    "PauseFeatureUpdatesEndTime",
                    "PauseQualityUpdatesStartTime",
                    "PauseQualityUpdatesEndTime",
                    "PauseUpdatesStartTime",
                    "PauseUpdatesExpiryTime",
                ]

                for value_name in values_to_delete:
                    try:
                        winreg.DeleteValue(key, value_name)
                        print(f"已删除注册表值: {value_name}")
                    except FileNotFoundError:
                        print(f"注册表值不存在: {value_name}")

            print("注册表操作执行完成")
            self.show_message("成功", "解除屏蔽成功")

        except Exception as e:
            print(f"发生错误: {e}")
            self.show_message("失败", str(e))

    def show_message(self, status, msg_flag):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setText(msg_flag)
        msg.setWindowTitle(status)
        msg.exec()


app = QApplication([])
# 使用QPixmap加载图像数据
qpixmap = QPixmap()
qpixmap.loadFromData(image_data)

app.setWindowIcon(QIcon(qpixmap))
ui = Windows()
# ui.window.setWindowFlag(Qt.FramelessWindowHint)  # 将界面设置为无框
# ui.window.setAttribute(Qt.WA_TranslucentBackground)  # 将界面属性设置为半透明
ui.window.show()
app.exec()
